package com.seu.project.seuquiz.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.seu.project.seuquiz.annotation.AuthCheck;
import com.seu.project.seuquiz.common.BaseResponse;
import com.seu.project.seuquiz.common.DeleteRequest;
import com.seu.project.seuquiz.common.ErrorCode;
import com.seu.project.seuquiz.common.ResultUtils;
import com.seu.project.seuquiz.exception.BusinessException;
import com.seu.project.seuquiz.model.converter.NoticeConverter;
import com.seu.project.seuquiz.model.dto.notice.NoticeAddRequest;
import com.seu.project.seuquiz.model.dto.notice.NoticeQueryRequest;
import com.seu.project.seuquiz.model.dto.notice.NoticeUpdateRequest;
import com.seu.project.seuquiz.model.entity.Notice;
import com.seu.project.seuquiz.service.NoticeService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 用户及其做题接口
 * @author SoftPro
 * @createDate: 2023-07-16 09:48
 */
@RestController
@RequestMapping("/notice")
@Api(tags = "公告管理接口")
@Slf4j
public class NoticeController {
    

    @Resource
    private NoticeService noticeService;

    // region_start 基本增删改查

    /**
     * 新增日志
     * @param request
     * @return
     */
    @PostMapping("/add")
    @AuthCheck(mustRole = "admin")
    public BaseResponse<Long> addNotice(@RequestBody NoticeAddRequest request) {
        if (request == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }

        String title = request.getTitle();
        String description = request.getDescription();
        String content = request.getContent();
        
        long id = noticeService.noticeAdd(title, description, content);
        return ResultUtils.success(id);
    }

    /**
     * 删除日志
     * @param deleteRequest
     * @return
     */
    @PostMapping("/delete")
    @AuthCheck(mustRole = "admin")
    public BaseResponse<Boolean> deleteNoticeById(@RequestBody DeleteRequest deleteRequest){
        if(deleteRequest==null || deleteRequest.getId()<0){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        boolean b = noticeService.removeById(deleteRequest.getId());
        return ResultUtils.success(b);
    }

    /**
     * 批量删除
     * @param deleteRequest
     * @return
     */
    @PostMapping("/deleteByIds")
    @AuthCheck(mustRole = "admin")
    public BaseResponse<Boolean> deleteNoticeByIds(@RequestBody DeleteRequest deleteRequest){
        if(deleteRequest==null || deleteRequest.getIds().size() < 0){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        boolean b = noticeService.removeByIds(deleteRequest.getIds());
        return ResultUtils.success(b);
    }

    /**
     * 更新日志
     * @param updateRequest
     * @return
     */
    @PostMapping("/update")
    @AuthCheck(mustRole = "admin")
    public BaseResponse<Boolean> updateNotice(@RequestBody NoticeUpdateRequest updateRequest){
        if(updateRequest==null || updateRequest.getId()<0){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        boolean b = noticeService.updateById(NoticeConverter.noticeQueryRequestToNotice(updateRequest));
        return ResultUtils.success(b);
    }

    /**
     * 根据id查询日志
     * @param id
     * @return
     */
    @GetMapping("/get")
    public BaseResponse<Notice> getNoticeById(int id){
        if(id<=0){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Notice UserNotice = noticeService.getById(id);
        return ResultUtils.success(UserNotice);
    }

    /**
     * 根据查询条件获取全部的日志信息  todo: 待测试
     * @param request
     * @return
     */
    @GetMapping("/list")
    public BaseResponse<List<Notice>> listNotice(NoticeQueryRequest request) {

        QueryWrapper<Notice> queryWrapper = noticeService.getQueryWrapper(request);
        List<Notice> list = noticeService.list(queryWrapper);
        return ResultUtils.success(list);
    }

    /**
     * 分页获取日志     todo:待测试
     * @param request
     * @return
     */
    @GetMapping("/list/page")
    public BaseResponse<Page<Notice>> listNoticeByPage(NoticeQueryRequest request) {
        long current = 1;
        long size = 10;
        if (request != null) {
            current = request.getCurrent();
            size = request.getPageSize();
        }
        QueryWrapper<Notice> queryWrapper = noticeService.getQueryWrapper(request);
        Page<Notice> page = noticeService.page(new Page(current, size), queryWrapper);
        return ResultUtils.success(page);
    }

    // region_end
}
